package com.jinsi.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jinsi.pojo.Equip;
import com.jinsi.pojo.ResultTable;
import com.jinsi.service.EquipService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/equip")
public class EquipController {


    @Autowired
    private EquipService equipService;

// 查询
    @RequestMapping("getAll")
    public ResultTable<Equip> getAll(){
        List<Equip> all = equipService.getAll();

        ResultTable<Equip> resultTable = new ResultTable<>();
        resultTable.setData(all);


        return resultTable;
    }


    // 分页展示
    @RequestMapping("getPage")
    public Map<String,Object> getPage(Integer page,Integer limit){
        Map<String, Object> map = new HashMap<>();
        // 开启分页
        PageHelper.startPage(page, limit);
        List<Equip> all =  equipService.getAll();
        //根据所有数据 创建PageInfo对象
       PageInfo<Equip> pageInfo = new PageInfo<>(all);

       map.put("total", pageInfo.getTotal());
       map.put("equips", pageInfo.getList());
       map.put("state",0);
       map.put("msg","成功");

        return map;
    }


    @RequestMapping("/add")
    public Map<String,String> addE(Equip equip){
        Map<String,String> map=  new HashMap<>();
        System.out.println(equip);

        try {
            equipService.addEquip(equip);

            map.put("eid",equip.getId()+"");
            map.put("ename",equip.getName());
           map.put("state","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();

            map.put("state","400");
            map.put("msg","添加失败");
        }

        return map;
    }

}
